package cn.rwklyd.BookKeeping.controller;

import cn.rwklyd.BookKeeping.pojo.Category;
import cn.rwklyd.BookKeeping.pojo.Result;
import cn.rwklyd.BookKeeping.pojo.dto.QueryCategories;
import cn.rwklyd.BookKeeping.service.CategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "分类管理", description = "分类的增删查改接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加分类
     */
    @PostMapping("/add")
    @Operation(summary = "添加分类", description = "添加一个新的分类")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> addCategory(@RequestBody Category category) {
        log.info("添加分类: {}", category);
        return categoryService.addCategory(category);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "根据ID删除分类")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> deleteCategory(
            @Parameter(description = "分类ID", required = true)
            @PathVariable Integer id) {
        log.info("删除分类: {}", id);
        return categoryService.deleteCategory(id);
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    @Operation(summary = "更新分类", description = "更新分类信息")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> updateCategory(@RequestBody Category category) {
        log.info("更新分类: {}", category);
        return categoryService.updateCategory(category);
    }

    /**
     * 多条件查询分类
     */
    @PostMapping("/query")
    @Operation(summary = "查询分类", description = "多条件查询分类，不传参数则查询所有")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<IPage<Category>> queryCategories(@RequestBody QueryCategories queryCategories) {
        log.info("查询分类参数: {}", queryCategories);
        Integer id = queryCategories.getId();
        String name = queryCategories.getName();
        Integer spend = queryCategories.getSpend();
        Integer userId = queryCategories.getUserId();
        Integer page = queryCategories.getPage() != null ? queryCategories.getPage() : 1;
        Integer pageSize = queryCategories.getPageSize() != null ? queryCategories.getPageSize() : 10;
        return categoryService.queryCategories(id, name, spend, userId, page, pageSize);
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取分类", description = "根据ID获取分类详情")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<Category> getCategoryById(
            @Parameter(description = "分类ID", required = true)
            @PathVariable Integer id) {
        log.info("获取分类: {}", id);
        return categoryService.getCategoryById(id);
    }

    /**
     * 更新分类状态（启用/禁用切换）
     */
    @PostMapping("/updateStatus/{id}")
    @Operation(summary = "切换分类状态", description = "切换分类的启用/禁用状态")
    @PreAuthorize("hasAuthority('view_users')")
    public Result<String> updateCategoryStatus(
            @Parameter(description = "分类ID", required = true)
          @PathVariable Integer id) {
        log.info("切换分类状态: id={}", id);
        return categoryService.updateCategoryStatus(id);
    }

} 